import { ApiResponse, User, UserProfile } from '@/types/data'
import { RootThunkAction } from '@/types/store'
import request from '@/utils/request'

export const getUser = (): RootThunkAction => {
//   console.log(data)
  return async (dispatch) => {
    const res = await request.get<ApiResponse<User>>('/user')
    console.log(res.data.data)
    dispatch({
      type: 'profile/getUser',
      payload: res.data.data
    })
  }
}

export const getUserProfile = (): RootThunkAction => {
  return async (dispatch) => {
    const res = await request.get<ApiResponse<UserProfile>>('/user/profile')
    console.log(res.data.data)
    dispatch({
      type: 'profile/getUserProfile',
      payload: res.data.data
    })
  }
}

// type 要修改的字段 （name, intro, gender, birthday）
// value 要改成的值
export const updateUserProfile = (type: string, value: string): RootThunkAction => {
  return async (dispatch) => {
    // 对象的属性名是变量的值
    await request.patch('/user/profile', { [type]: value })
    // console.log(res)
    dispatch(getUserProfile())
  }
}

// 更新用户头像
export const updateUserPhoto = (formDate: FormData): RootThunkAction => {
  return async (dispatch) => {
    await request.patch('/user/photo', formDate)
    // 重发请求
    dispatch(getUserProfile())
  }
}
